package com.thinkingstar.iads.projectcenter.entity.dao;

import com.thinkingstar.iads.common.jsonUtils.AjaxResult;
import com.thinkingstar.iads.common.serviceImpl.EntityRepositoryServiceImpl;
import com.thinkingstar.iads.datacenter.entity.*;
import com.thinkingstar.iads.projectcenter.entity.PcProjectConstantData;
import com.thinkingstar.iads.projectcenter.entity.PcProjectFormulaData;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.Sqls;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Criteria;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


/**
 * @ClassName: DaoPcProjectData 
 * @Description:项目中心_项目_常量维护
 * @author z
 * @date 2018年5月24日
 */
@IocBean(args = {"refer:dao"})
public class DaoPcProjectConstantData extends EntityRepositoryServiceImpl<PcProjectConstantData>{

	public DaoPcProjectConstantData(Dao dao) {
		super(dao);
	}

	/**
	 * 根据project_id查询是否已经取用常量
	 * @param project_id
	 * */
	public boolean isHasQyConstant(String project_id){
		String executeSql = "select * from pc_projecr_constant_data where project_id=@project_id ";
		Sql sql = Sqls.create(executeSql);
		sql.params().set("project_id", project_id);
		sql.setCallback(Sqls.callback.entities());
		sql.setEntity(dao().getEntity(PcProjectConstantData.class));
		dao().execute(sql);
		List<PcProjectConstantData> list = sql.getList(PcProjectConstantData.class);
		if (list.size()>0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 根据constant_version_id查询数据中心常量集
	 * */
	public DcProjectDesignConstantVersion getDcProjectConstantVersionById(String constant_version_id) {
		Criteria cri = Cnd.NEW();
		cri.where().and("id", "=", constant_version_id);
		List<DcProjectDesignConstantVersion> pcProjectConstantVersionList = dao().query(DcProjectDesignConstantVersion.class, cri);
		return pcProjectConstantVersionList.size()>0?pcProjectConstantVersionList.get(0):null;
	}

	/**
	 *	查询数据中心所有符合条件的常量集 列表(  不带分页)(old)
	 *@param  content_id	  设计内容id
	 * @param  phase_id  设计阶段id
	 * */
	public List<DcProjectDesignConstantVersion> getDcProjectConstantVersion(String content_id,String phase_id) {
		String executeSql="select * from dc_constant_version where project_design_content_id=@content_id and project_design_phase_id=@phase_id and del_flag='0' order by constant_table_version_number desc ";
		Sql sql = Sqls.create(executeSql);
		sql.params().set("content_id",content_id);
		sql.params().set("phase_id", phase_id);
		dao().execute(sql);
		sql.setCallback(Sqls.callback.entities());
		sql.setEntity(dao().getEntity(DcProjectDesignConstantVersion.class));
		dao().execute(sql);
		return sql.getList(DcProjectDesignConstantVersion.class);
	}

	/**
	 *	查询数据中心所有符合条件的常量集 列表(  不带分页)( new)
	 *@param  content_id	  设计内容id
	 * @param  phase_id  设计阶段id
	 * */
	public List<DcProjectDesignConstantVersion> getDcProjectConstantVersion(String content_id,String phase_id,String design_cell_id) {
		String executeSql="select v.* from dc_constant_version v where v.project_design_content_id=@content_id and v.project_design_phase_id=@phase_id and del_flag='0'" +
				" and locate(v.id,(select c.constant_version_ids from dc_project_design_cell c where c.id=@design_cell_id ))>0 " +
				"  order by constant_table_version_number desc ";
		Sql sql = Sqls.create(executeSql);
		sql.params().set("content_id",content_id);
		sql.params().set("phase_id", phase_id);
		sql.params().set("design_cell_id",design_cell_id);
		dao().execute(sql);
		sql.setCallback(Sqls.callback.entities());
		sql.setEntity(dao().getEntity(DcProjectDesignConstantVersion.class));
		dao().execute(sql);
		return sql.getList(DcProjectDesignConstantVersion.class);
	}

	/**
	 *	查询数据中心所有符合条件的常量集 列表(带分页)
	 *@param  param	  设计内容id
	 * @param  param  设计阶段id
	 * */
	public QueryResult getDcProjectConstantVersionPage(NutMap param) {
		List <NutMap> page_list = null;
		Pager pager = null;
		String executeSql="select a.* from (select *,case (" +
				" select count(*) from pc_project_constant_data where project_id=@project_id  and version_id=dc.id " +
				" and del_flag='0' " +
				" )  when '0' then '0' else '1' end isQy from dc_constant_version dc where project_design_content_id=@content_id " +
				"and project_design_phase_id=@phase_id and del_flag='0' order by constant_table_version_number desc ) " +
				" a where locate(a.id,(select c.constant_version_ids from dc_project_design_cell c where c.id=@design_cell_id))";
		Sql sql = Sqls.create(executeSql);
		sql.params().set("project_id", param.getString("project_id"));
		sql.params().set("content_id", param.getString("content_id"));
		sql.params().set("phase_id", param.getString("phase_id"));
		sql.params().set("design_cell_id",param.getString("design_cell_id"));
		if(param.get("page_num") != null && param.get("page_size") != null){
			pager = new Pager(param.getInt("page_num"), param.getInt("page_size"));
			pager.setRecordCount(getDcProjectConstantVersionCount(param));
			sql.setPager(pager);
		}
		sql.setCallback(new SqlCallback() {
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				List<NutMap> list = new ArrayList<>();
				while (rs.next()){
					NutMap map = new NutMap();
					ResultSetMetaData metaData = rs.getMetaData();
					int count = metaData.getColumnCount();
					for (int i = count; i > 0; i--) {
						String filed = metaData.getColumnName(i);
						map.put(filed, rs.getString(filed));
					}
					list.add(map);
				}
				return list;
			}
		});
		dao().execute(sql);
		page_list = sql.getList(NutMap.class);
		return new QueryResult(page_list,pager);
	}

	public int getDcProjectConstantVersionCount(NutMap param) {
		String executeSql="select count(*) num from dc_constant_version v where v.project_design_content_id=@content_id and v.project_design_phase_id=@phase_id and v.del_flag='0' " +
				" and locate(v.id,(select c.constant_version_ids from dc_project_design_cell c where c.id=@design_cell_id))>0  " +
				" order by constant_table_version_number desc ";
		Sql sql = Sqls.create(executeSql);
		sql.params().set("content_id", param.getString("content_id"));
		sql.params().set("phase_id", param.getString("phase_id"));
		sql.params().set("design_cell_id",param.getString("design_cell_id"));
		sql.setCallback(new SqlCallback() {
			int num = 0;
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				while (rs.next()) {
					num = rs.getInt("num");
				}
				return num;
			}
		});
		dao().execute(sql);
		return sql.getInt();
	}


	/**
	 *	查询数据中心指定常量集下的所有常量和代码
	 *@param  	version_id  公式集ID
	 * */
	public List<DcProjectDesignConstant> getDcProjectConstantDataList(String version_id) {
		String executeSql="select * from dc_constant_data where constant_version_id=@version_id and del_flag='0'";
		Sql sql = Sqls.create(executeSql);
		sql.params().set("version_id", version_id);
		sql.setCallback(Sqls.callback.entities());
		sql.setEntity(dao().getEntity(DcProjectDesignConstant.class));
		dao().execute(sql);
		return sql.getList(DcProjectDesignConstant.class);
	}


	/**
	 *	查询项目中心 指定常量集code 是否已取用
	 *@param  	project_id  项目id
	 * @param  version_id  常量集id
	 * */
	public boolean getPcProjectConstantVersionIsHasQy(String project_id,String version_id) {
		String executeSql="select * from pc_project_constant_data where project_id=@project_id and version_id=@version_id and del_flag='0'";
		Sql sql = Sqls.create(executeSql);
		sql.params().set("project_id", project_id);
		sql.params().set("version_id", version_id);
		sql.setCallback(Sqls.callback.entities());
		sql.setEntity(dao().getEntity(PcProjectConstantData.class));
		dao().execute(sql);
		return sql.getList(PcProjectConstantData.class).size()==0?false:true;
	}

	/**
	 * 根据项目id查询项目中心所有常量和代码(old)
	 * @param project_id 项目id
	 * @return
	 */
	public List<PcProjectConstantData> getPcProjectConstantDataList(String project_id){
		Criteria cri = Cnd.NEW();
		cri.where().and("project_id", "=", project_id);
		List<PcProjectConstantData> pcProjectConstantDataList = dao().query(PcProjectConstantData.class, cri);
		return pcProjectConstantDataList;
	}

	/**
	 * 根据项目id和设计单元id查询项目中心所有常量和代码(new)
	 * @param project_id 项目id
	 * @return
	 */
	public List<PcProjectConstantData> getPcProjectConstantData(String project_id,String design_cell_id){
		Criteria cri = Cnd.NEW();
		cri.where().and("project_id", "=", project_id).and("design_cell_id","=",design_cell_id);
		List<PcProjectConstantData> pcProjectConstantDataList = dao().query(PcProjectConstantData.class, cri);
		return pcProjectConstantDataList;
	}


	/**
	 * 根据常量集code和项目id查询项目中心其下所有常量和代码
	 * @param constant_table_code 常量集code
	 * @param project_id 项目id
	 * @return
	 */
	public List<PcProjectConstantData> getPcProjectConstantDataList(String constant_table_code, String project_id){
		Criteria cri = Cnd.NEW();
		cri.where().and("constant_table_code", "=", constant_table_code);
		cri.where().and("project_id", "=", project_id);
		List<PcProjectConstantData> pcProjectConstantDataList = dao().query(PcProjectConstantData.class, cri);
		return pcProjectConstantDataList;
	}

	/**
	 * 根据常量集code和项目id查询项目中心其下所有常量和代码  (带分页)
	 * @param param 常量集code
	 * @param param 项目id
	 * @return
	 */
	public  QueryResult getPcProjectConstantDataListPage(NutMap param){
		List <NutMap> page_list = null;
		Pager pager = null;
		String executeSql="select * from pc_project_constant_data where project_id=@project_id and constant_table_code=@constant_table_code " ;
		Sql sql = Sqls.create(executeSql);
		sql.params().set("project_id", param.getString("project_id"));
		sql.params().set("constant_table_code", param.getString("constant_table_code"));
		if(param.get("page_num") != null && param.get("page_size") != null){
			pager = new Pager(param.getInt("page_num"), param.getInt("page_size"));
			pager.setRecordCount(getPcProjectConstantDataCount(param));
			sql.setPager(pager);
		}
		sql.setCallback(new SqlCallback() {
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				List<NutMap> list = new ArrayList<>();
				while (rs.next()){
					NutMap map = new NutMap();
					ResultSetMetaData metaData = rs.getMetaData();
					int count = metaData.getColumnCount();
					for (int i = count; i > 0; i--) {
						String filed = metaData.getColumnName(i);
						map.put(filed, rs.getString(filed));
					}
					list.add(map);
				}
				return list;
			}
		});
		dao().execute(sql);
		page_list = sql.getList(NutMap.class);
		return new QueryResult(page_list,pager);
	}
	/**
	 * 查询项目中心指定常量集下的常量数量
	 * @param param
	 * @return
	 */
	public int getPcProjectConstantDataCount(NutMap param) {
		String executeSql="select count(*) num from pc_project_constant_data where project_id=@project_id and constant_table_code=@constant_table_code and del_flag='0' ";
		Sql sql = Sqls.create(executeSql);
		sql.params().set("project_id", param.getString("project_id"));
		sql.params().set("constant_table_code", param.getString("constant_table_code"));
		sql.setCallback(new SqlCallback() {
			int num = 0;
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				while (rs.next()) {
					num = rs.getInt("num");
				}
				return num;
			}
		});
		dao().execute(sql);
		return sql.getInt();
	}

	/**
	 * 根据常量库code查询项目中心该常量集下的所有常量和代码
	 * @param version_id
	 * @return
	 */
	public List<PcProjectConstantData> getConstantDataByVersionId(String version_id){
		Criteria cri = Cnd.NEW();
		cri.where().and("version_id", "=", version_id);
		List<PcProjectConstantData> pcProjectConstantDataList = dao().query(PcProjectConstantData.class, cri);
		return pcProjectConstantDataList;
	}

	/**
	 * 删除项目中心已取用某一常量集下的所有常量
	 * */
	public AjaxResult deletePcProjectConstantByOne(String project_id,String constant_table_code) {
		String executeSql="delete from pc_project_constant_data where project_id=@project_id and constant_table_code=@constant_table_code and del_flag='0'";
		Sql sql = Sqls.create(executeSql);
		sql.params().set("project_id", project_id);
		sql.params().set("constant_table_code", constant_table_code);
		sql.setCallback(Sqls.callback.entities());
		sql.setEntity(dao().getEntity(PcProjectConstantData.class));
		dao().execute(sql);
		return AjaxResult.success();
	}

	/**
	 * 删除项目中心已取用的所有常量
	 * */
	public AjaxResult deletePcProjectConstantBatch(String project_id,String design_cell_id) {
		String executeSql="delete from pc_project_constant_data where project_id=@project_id " +
				"and design_cell_id=@design_cell_id ";
		Sql sql = Sqls.create(executeSql);
		sql.params().set("project_id", project_id);
		sql.params().set("design_cell_id",design_cell_id);
		sql.setCallback(Sqls.callback.entities());
		sql.setEntity(dao().getEntity(PcProjectConstantData.class));
		dao().execute(sql);
		return AjaxResult.success();
	}

	/**
	 * 将数据中心公式list插入项目中心
	 * */
	public AjaxResult insertConstantBatch(List<DcProjectDesignConstant> list, String project_id,String design_cell_id) {
		List<PcProjectConstantData> list2 = new ArrayList<PcProjectConstantData>();
		for(int i=0;i<list.size();i++) {
			//根据formula_version_id查询formula_table_code
			DcProjectDesignConstantVersion version = getDcProjectConstantVersionById(list.get(i).getConstant_version_id());
			if (null!=version) {
				PcProjectConstantData ppfd = new PcProjectConstantData();
				ppfd.setProject_id(project_id);
				ppfd.setVersion_id(version.getId());
				ppfd.setConstant_table_code(version.getConstant_table_code());
				ppfd.setCode_cname(list.get(i).getCode_cname());
				ppfd.setCode_default_value(list.get(i).getCode_default_value());
				ppfd.setCode_name(list.get(i).getCode_name());
				ppfd.setCode_parent_name(list.get(i).getCode_parent_name());
				ppfd.setCode_value(list.get(i).getCode_value());
				ppfd.setCode_value_unit(list.get(i).getCode_value_unit());
				ppfd.setHas_children(list.get(i).getHas_children());
				ppfd.setRemark(list.get(i).getRemark());
				ppfd.setStandard_type(list.get(i).getStandard_type());
				ppfd.setDesign_cell_id(design_cell_id);
				list2.add(ppfd);
			}
		}
		for(int i=0;i<list2.size();i++) {
			dao().insert(list2.get(i));
		}
		return AjaxResult.success();
	}


	/**
	 * 根据常量id获取常量详情编辑
	 * @param id
	 * @return
	 */
	public PcProjectConstantData getConstantDataById(String id){
		Criteria cri = Cnd.NEW();
		cri.where().and("id", "=", id);
		PcProjectConstantData pcProjectConstantData = dao().query(PcProjectConstantData.class, cri).get(0);
		return pcProjectConstantData;
	}


	/**
	 * 根据项目id
	 * */
	/**
	 * 更新对象
	 * @param pcProjectConstantData
	 * @return
	 */
	public int updateProjectMention(PcProjectConstantData pcProjectConstantData){
		return updateObj(pcProjectConstantData);
	}

}
